package com.graph.servcie.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.graph.pojo.Point;
import com.graph.servcie.GraphService;
import com.graph.test.NodeRelation;
import com.graph.test.Relation;
import com.graph.test.ShowGarphVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GraphServiceImpl implements GraphService {

    private final String urlPrefix = "http://localhost:5001";

    private final String GET_RELATE_NODE_FULL = urlPrefix+"/get_relate_node_full?name=";
    private final String GET_RELATE_NODE = urlPrefix+"/get_relate_node?name=";
    private final String GET_NODE_INFO = urlPrefix+"/get_node_info?name=";

    @Autowired
    private RestTemplate restTemplate ;

    @Override
    public ShowGarphVo getRelateNodeFull(String nodeName) {
        Map<String,String> params = new HashMap<>();
        params.put("name",nodeName);
        String forObject = restTemplate.getForObject(GET_RELATE_NODE_FULL+nodeName,String.class);
        //String forObject = restTemplate.getForObject("http://localhost:5001/get_relate_node_full",String.class,params);

        //开始解析
        System.out.println(forObject);
        return parseJSONString(forObject);
    }



    @Override
    public ShowGarphVo findAll() {
        System.out.println("hello");
        //试着解析上面的的json字符串
        String json = "[{\n" +
                "\t\t\"a\": {\n" +
                "\t\t\t\"name\": \"node1\"\n" +
                "\t\t},\n" +
                "\t\t\"labels(a)\": [\"Im N1\"],\n" +
                "\t\t\"r\": [{\n" +
                "\t\t\t\t\"name\": \"node1\"\n" +
                "\t\t\t},\n" +
                "\t\t\t\"1->2\",\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"name\": \"node2\"\n" +
                "\t\t\t}\n" +
                "\t\t],\n" +
                "\t\t\"b\": {\n" +
                "\t\t\t\"name\": \"node2\"\n" +
                "\t\t},\n" +
                "\t\t\"labels(b)\": [\"Im N2\",\"Im dddd\"]\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"a\": {\n" +
                "\t\t\t\"name\": \"node2\"\n" +
                "\t\t},\n" +
                "\t\t\"labels(a)\": [\"Im N2\" ,\"Im dddd\" ],\n" +
                "\t\t\"r\": [{\n" +
                "\t\t\t\t\"name\": \"node2\"\n" +
                "\t\t\t},\n" +
                "\t\t\t\"2->3\",\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"name\": \"node3\"\n" +
                "\t\t\t}\n" +
                "\t\t],\n" +
                "\t\t\"b\": {\n" +
                "\t\t\t\"name\": \"node3\"\n" +
                "\t\t},\n" +
                "\t\t\"labels(b)\": [\"Im N3\"]\n" +
                "\t}\n" +
                "]";

        return parseJSONString(json);
    }

    private ShowGarphVo parseJSONString(String json) {
        //使用fastjson解析这个字符串
        JSONArray jsonArray = JSON.parseArray(json);
        List<Point> pointList = new ArrayList<>();
        List<Relation> relationList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            String sjson = jsonArray.get(i).toString();
            String jsonStr = sjson.replace("labels(a)", "labels_a").replace("labels(b)", "labels_b");
            System.out.println(jsonStr);

            NodeRelation nodeRelation = JSON.parseObject(jsonStr, NodeRelation.class);
            System.out.println(nodeRelation);
            //提取出里面的所有Node，里面可能存在重复元素
            List<NodeRelation.Node> nodes = nodeRelation.getNodes();
            List<Point> nodes2pointList = nodes2points(nodes);
            pointList.addAll(nodes2pointList);

            //处理关系 数组的第一个元素是一个对象
            Object[] r = nodeRelation.getR();
            NodeRelation.Node source = JSON.parseObject(r[0].toString(), NodeRelation.Node.class);
            String value = r[1].toString();
            NodeRelation.Node target = JSON.parseObject(r[2].toString(), NodeRelation.Node.class);
            Relation relation = new Relation();
            relation.setSource(source.getName());
            relation.setValue(value != null ? value : "");
            relation.setTarget(target.getName());
            relationList.add(relation);

        }
        System.out.println(pointList.size());
        //使用Lambda为拿到nodeList中Node对象的name属性转为数组
        //List<String> nodeNameList = nodeList.stream().map(NodeRelation.Node::getName).collect(Collectors.toList());
        //为nodeList去重 stream流的distincet方法比较的是比较的地址值
        pointList = pointList.stream().distinct().collect(Collectors.toList());

        ShowGarphVo showGarphVo = new ShowGarphVo();
        showGarphVo.setData(pointList);
        showGarphVo.setLinks(relationList);
        return showGarphVo;
    }



    /**
     * 将nodes集合转为points集合，二者的value字段类型不同
     * @param nodes
     * @return
     */
    private List<Point> nodes2points(List<NodeRelation.Node> nodes) {
        if (CollectionUtils.isEmpty(nodes)) return null;
        List<Point> points = new ArrayList<>();
        for (NodeRelation.Node node : nodes) {
            Point point = new Point();
            point.setName(node.getName());
            String[] value = node.getValue();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < value.length; i++) {
                sb.append(value[i]);
                if (value.length != 1) {
                    if (i == value.length - 1) {
                        break;
                    }
                    sb.append("-");
                }
            }
            point.setValue(sb.toString());
            points.add(point);
        }
        return points;
    }
}
